package com.yuanluludev.lib_pic.utils

import android.content.Context
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.yuanluludev.lib_pic.GlideEngine
import com.yuanluludev.lib_pic.ImageFileCompressEngine
import com.yuanluludev.lib_pic.bean.PicSelectorType


/**
 * yuanLulu
 * 选择图片或者视频
 */
object PicSelectorUtils {

    fun choose(
        context: Context,
        type: PicSelectorType,
        maxSelectNum: Int = 9,
        selectData: ArrayList<LocalMedia>,
        requestCode: Int = PictureConfig.CHOOSE_REQUEST,
        isMakeVideoThumbnail: Boolean = true,
    ) {
        PictureSelector.create(context)
            .openGallery(
                when (type) {
                    PicSelectorType.TYPE_IMAGE_VIDEO -> SelectMimeType.ofAll()
                    PicSelectorType.TYPE_IMAGE -> SelectMimeType.ofImage()
                    PicSelectorType.TYPE_VIDEO -> SelectMimeType.ofVideo()
                }
            ).apply {
                when (type) {

                    //视频、图片同选
                    PicSelectorType.TYPE_IMAGE_VIDEO -> {
                        isWithSelectVideoImage(true)
                        //图片、视频都可选择的时候 设置最大视频选择数量
                        setMaxVideoSelectNum(maxSelectNum)
                        //设置缩略图
                        if (isMakeVideoThumbnail) setVideoThumbnailListener(MeOnVideoThumbnailEventListener(context))
                    }

                    PicSelectorType.TYPE_IMAGE -> isPreviewImage(true)

                    PicSelectorType.TYPE_VIDEO -> {
                        isPreviewVideo(true)
                        //设置缩略图
                        if (isMakeVideoThumbnail) setVideoThumbnailListener(MeOnVideoThumbnailEventListener(context))
                    }

                }
            }
            .setSelectorUIStyle(SelectorUIStyleUtils.getWeChatUiStyle(context))
            .setImageEngine(GlideEngine.createGlideEngine())
            .setCompressEngine(ImageFileCompressEngine())
            .setMaxSelectNum(maxSelectNum)
            .setSelectedData(selectData)
            .forResult(requestCode)
    }

    /**
     * 拍照
     */
    fun camera(
        context: Context,
        listener: OnResultCallbackListener<LocalMedia>,
    ) {
        PictureSelector.create(context)
            .openCamera(SelectMimeType.ofImage())
            .setCompressEngine(ImageFileCompressEngine())
            .forResult(listener)
    }

}